package cn.lili.controller.system;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.system.entity.dos.Logistics;
import cn.lili.modules.system.entity.dos.SupplyChain;
import cn.lili.modules.system.entity.dos.SupplyChainAccess;
import cn.lili.modules.system.entity.vo.SupplyChainAccessVo;
import cn.lili.modules.system.service.LogisticsService;
import cn.lili.modules.system.service.SupplyChainAccessService;
import cn.lili.modules.system.service.SupplyChainService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 供应链接口
 *
 * @author Bulbasaur
 * @since 2020-05-5 15:10:16
 */
@RestController
@Api(tags = "供应商端,供应链接口")
@RequestMapping("/system/supplychain")
@RequiredArgsConstructor
public class SupplyChainController {


    private final SupplyChainService supplyChainService;

    private final SupplyChainAccessService supplyChainAccessService;


    @ApiOperation(value = "获取供应链信息")
    @GetMapping("/list")
    public ResultMessage<List<SupplyChain>> getList() {
        return ResultUtil.data(supplyChainService.getOpenSupplyChains());
    }

    @ApiOperation(value = "通过id获取供应链")
    @GetMapping(value = "/{id}")
    public ResultMessage<SupplyChain> get(@PathVariable String id) {
        return ResultUtil.data(supplyChainService.getById(id));
    }

    @ApiOperation(value = "分页获取供应链")
    @GetMapping
    public ResultMessage<Page<SupplyChain>> getByPage(PageVO page) {
        return ResultUtil.data(supplyChainService.page(PageUtil.initPage(page)));
    }

    @ApiOperation(value = "编辑供应链")
    @ApiImplicitParam(name = "id", value = "供应链ID", required = true, paramType = "path", dataType = "string")
    @PutMapping(value = "/{id}")
    public ResultMessage<SupplyChain> update(@NotNull @PathVariable String id,
                                             @Valid SupplyChain supplyChain) {
        supplyChain.setId(id);
        supplyChainService.updateById(supplyChain);
        return ResultUtil.data(supplyChain);
    }

    @ApiOperation(value = "添加供应链")
    @PostMapping
    public ResultMessage<SupplyChain> save(@Valid SupplyChain supplyChain) {
        supplyChain.setStatus(1);
        supplyChain.setCreateTime(new Date());
        supplyChainService.save(supplyChain);
        return ResultUtil.data(supplyChain);
    }

    @ApiOperation(value = "删除供应链")
    @ApiImplicitParam(name = "id", value = "供应链ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping(value = "/{id}")
    public ResultMessage<Object> delAllByIds(@PathVariable String id) {
        supplyChainService.removeById(id);
        return ResultUtil.success();
    }


    @ApiOperation(value = "编辑供应链状态")
    @ApiImplicitParam(name = "id", value = "供应链ID", required = true, paramType = "path", dataType = "string")
    @PutMapping(value = "/status/{id}")
    public ResultMessage<SupplyChain> updateStatus(@NotNull @PathVariable String id,
                                             @Valid SupplyChain supplyChain) {
        Integer status = supplyChain.getStatus();
        System.out.println("===============status =="+status);
        supplyChainService.updateStatus(id,status);
        supplyChain = supplyChainService.getById(id);
        return ResultUtil.data(supplyChain);
    }



    @ApiOperation(value = "获取分配给供应商的供应链")
    @GetMapping(value = "/supplier/{supplierId}")
    public ResultMessage<List<SupplyChain>> getSupplierAccessChain(@NotNull @PathVariable String supplierId) {
        List<SupplyChainAccess> accessList = supplyChainAccessService.getListBySupplierId(supplierId);
        List<SupplyChain> supplyChainList = new ArrayList<>();
        if(null!=accessList){
            List<String> chainIdList = accessList.stream().map(item->item.getSupplyChainId()).collect(Collectors.toList());
            if(!chainIdList.isEmpty()){
                supplyChainList = supplyChainService.listBy(new String[]{"id","name","short_name"},chainIdList);
            }
            return ResultUtil.data(supplyChainList);
        }
        return ResultUtil.data(supplyChainList);
    }




    @ApiOperation(value = "保存分配给供应商的供应链")
    @PostMapping(value = "/saveSupplierAccessChain/{supplierId}")
    public ResultMessage<Object> saveSupplierAccessChain(@NotNull @PathVariable String supplierId,
                                                         SupplyChainAccessVo supplyChainAccessVo) {
        List<String> supplyChainIdList = supplyChainAccessVo.getSupplyChainIdList();
        if(null == supplyChainIdList || supplyChainIdList.size()<1){
            return ResultUtil.error(500,"缺少参数");
        }
        supplyChainAccessService.saveChainAccessList(supplierId,supplyChainIdList);
        return ResultUtil.success();
    }
}
